package com.punai.common.utils;

import org.apache.ibatis.type.BigDecimalTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;

import java.math.BigDecimal;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
 * 统一处理 Mybatis返回的BigDecimal的尾数范围
 *
 * @author tj
 * @date 2023-04-12
 */
@MappedJdbcTypes(JdbcType.NUMERIC)
public class SubBigDecimalTypeHandler extends BigDecimalTypeHandler {
    @Override
    public BigDecimal getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return super.getNullableResult(rs,columnName)!= null ? super.getNullableResult(rs,columnName).setScale(3,BigDecimal.ROUND_HALF_UP) : super.getNullableResult(rs,columnName);
    }
    @Override
    public BigDecimal getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return super.getNullableResult(rs,columnIndex)!=null ? super.getNullableResult(rs,columnIndex).setScale(3,BigDecimal.ROUND_HALF_UP) : super.getNullableResult(rs,columnIndex);
    }
    @Override
    public BigDecimal getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return super.getNullableResult(cs,columnIndex)!=null ? super.getNullableResult(cs,columnIndex).setScale(3,BigDecimal.ROUND_HALF_UP) : super.getNullableResult(cs,columnIndex);
    }
}
